package com.ayf.payment.game.api.utils;

import java.security.*;

/**
 * RSA公私钥工具类
 */
public class RSAKeyGenerateUtil {
    private static String ALGORITHM="RSA";
    private static PrivateKey pkey ;
    private static PublicKey pubkey ;

    public static class RSAKey {
        public RSAKey(String pubKey, String prikey) {
            this.pubKey = pubKey;
            this.prikey = prikey;
        }

        private String pubKey;
        private String prikey;

        public String getPubKey() {
            return pubKey;
        }

        public void setPubKey(String pubKey) {
            this.pubKey = pubKey;
        }

        public String getPrikey() {
            return prikey;
        }

        public void setPrikey(String prikey) {
            this.prikey = prikey;
        }
    }


    public static RSAKey genKey() {
        KeyPairGenerator kpg = null;
        try {
            kpg = KeyPairGenerator.getInstance(ALGORITHM);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        kpg.initialize(2048);
        KeyPair kep = kpg.generateKeyPair();
        Provider p  = kpg.getProvider();
        pkey = kep.getPrivate();
        pubkey = kep.getPublic();
        String pubKey = new String(Base64.encode(pubkey.getEncoded()));
        String prikey= new String(Base64.encode(pkey.getEncoded()));
        RSAKey rsaKey = new RSAKey(pubKey,prikey);
        return rsaKey;
    }
}
